package com.wei.stavix.ui.module.account

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.navigation.compose.rememberNavController
import com.wei.stavix.ui.theme.JetComposeTheme
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.observeOn
import kotlinx.coroutines.flow.onSubscription
import kotlinx.coroutines.launch

class AccountActivity : AppCompatActivity(), CoroutineScope by MainScope() {

    private val viewModel by viewModels<AccountViewModel>()

    fun isHarmonyOs(): Boolean {
        return try {
            val cls = Class.forName("ohos.utils.system.SystemCapability")
            cls != null
        } catch (e: Exception) {
            false
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.i("compose", "isHarmonyOs:${isHarmonyOs()}")
        setContent {
            JetComposeTheme {
                Surface(color = MaterialTheme.colors.background) {
//                    LoginPageView(viewModel)
//                    if (viewModel.pageShowFlag > 0) {
//                        RegisterPageView(viewModel)
//                    }
                    AccountHost(viewModel = viewModel)
                }
            }
        }

        launch {
//            viewModel.onBackDown2.observe(this@AccountActivity, {
//
//            })
            viewModel.onBackDown.collect {
                Log.i("compose", "onBackDown :$it")
                if (it != 0) {
                    onBackPressed()
                }
            }
        }
    }

    override fun onBackPressed() {
        Log.i("compose", "onBackPressed :${viewModel.pageShowFlag}")
//        if (viewModel.pageShowFlag > 0) {
//            viewModel.pageShowFlag = 0
//            return
//        }
        super.onBackPressed()
    }
}